package com.guxingke.typecho4j.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期相关操作的工具类
 * 
 * @author guxingke
 *
 */
public class DateUtil {

	private DateUtil() {
	}

	/**
	 * 获取当前时间的unix时间戳
	 * 
	 * @return
	 */
	public static Long getUnixTimestamp() {
		return System.currentTimeMillis() / 1000;
	}

	/**
	 * 获取unix时间戳对应的格式化时间字符串
	 * 
	 * @param unixTimestamp
	 * @param format
	 * @return
	 */
	public static String getDateFormatString(Long unixTimestamp,
			String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = new Date(unixTimestamp * 1000);
		return sdf.format(date);
	}

	/**
	 * 获取某年第一天
	 * */
	public static Long getYearFirstDay(int year) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, 0);
		cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTimeInMillis() / 1000;
	}

	/**
	 * 获取某年某月的第一天
	 * */
	public static Long getMonthFirstDay(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTimeInMillis() / 1000;
	}

	/**
	 * 获取某年某月的某一天
	 * */
	public static Long getDayFirstTime(int year, int month, int day) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DAY_OF_MONTH, day);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTimeInMillis() / 1000;
	}

	/*
	 * public static Long getUnixTimestamp(String s, String format) {
	 * SimpleDateFormat sdf = new SimpleDateFormat(format);
	 * 
	 * Long result = null; try { result = sdf.parse(s).getTime() / 1000; }
	 * catch (ParseException e) { e.printStackTrace(); } return result; }
	 */
}
